// Macquarie Website Hacks
// version 0.1
// Copyleft (c) 2010, Ryan Lie
// Released under the GPL license
// http://www.gnu.org/copyleft/gpl.html
//
// --------------------------------------------------------------------
//
// This is a Greasemonkey user script.  To install it, you need
// Greasemonkey 0.3 or later: http://greasemonkey.mozdev.org/
// Then restart Firefox and revisit this script.
// Under Tools, there will be a new menu item to "Install User Script".
// Accept the default configuration and install.
//
// To uninstall, go to Tools/Manage User Scripts,
// select "Hello World", and click Uninstall.
//
// --------------------------------------------------------------------
//
// ==UserScript==
// @name          mqfox
// @description   tweaks to Macquarie University's websites
// @namespace     http://code.google.com/p/mqfox
// @include       https://moodle.comp.mq.edu.au/*
// @include       http://mq.maharasites.com/*
// ==/UserScript==

/*
 * taken from http://snipplr.com/view/19838/get-url-parameters/
 * Read a page's GET URL variables and return them as an associative array.
 */
function getUrlVars() {
	var vars = {};
	var parts = document.location.href.replace(/[?&]+([^=&]+)=([^&]*)/gi, function(m,key,value) {
		vars[key] = value;
	});
	return vars;
}

var urlvars = getUrlVars();
var loc = document.location;
if (loc.host == "moodle.comp.mq.edu.au" 
    && loc.pathname == "/mod/forum/post.php") {

    var site = "moodle";
    var id = urlvars.forum ? "post" + urlvars.forum : "reply" + urlvars.reply

    // the text box for subject line
    var subject = document.getElementById("id_subject");

    // the main message textbox 
    var htmlarea = document.getElementsByClassName("htmlarea");
    // if (htmlarea .length != 1) alert("mqfox: wrong number of textbox found, found: " + htmlarea.length);
    var iframes = htmlarea[0].getElementsByTagName("iframe");
    // if (iframes.length != 1) alert("mqfox: wrong number of iframes found");
    var iframe = iframes[0];
    var content = iframe.contentDocument;

    // the main message textbox when formatting is disabled
    var message = document.getElementById("id_message");


    // restores the subject line and message
    subject.value = GM_getValue(site+"_subject_"+id, "");
    content.body.innerHTML = GM_getValue(site+"_message_"+id, "");

    // installs auto-saver
    content.addEventListener("blur", 
        function() {
            GM_setValue(site+"_subject_"+id, subject.value);
            GM_setValue(site+"_message_"+id, content.body.innerHTML);
        },
        true
    );
} else if (loc.host == "mq.maharasites.com") {

    var type = loc.pathname == "/interaction/forum/edittopic.php" ? "post" :
               loc.pathname == "/interaction/forum/editpost.php" ?  "reply" :
                                                                    "unknown";
    if (type != "unknown") {

        var site = "mahara";
        var id = type + (type == "post" ? urlvars.forum : urlvars.parent);

        // the text box for subject line
        var subject = document.getElementById("addtopic_subject");

        // the main message textbox 
        var elemname = type == "post" ? "addtopic_body" : "editpost_body";
        var message = document.getElementById(elemname);

        if (subject) subject.value = GM_getValue(site+"_subject_"+id, "");
        if (message) message.value = GM_getValue(site+"_message_"+id, "");

        tmce = document.getElementById("tinymce");
        // FIXME: find better autosaver hooks
        if (subject) {
            subject.addEventListener("blur",
                function() {
                    GM_setValue(site+"_subject_"+id, subject.value);
                },
                true
            );
        } else {
            document.addEventListener("blur",
                function() {
                    GM_setValue(site+"_message_"+id, tmce.innerHTML);
                },
                true
            );
        }
        
    }
}
